package managedbean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import model.Category;
import dao.CategoryDao;

@ManagedBean(name="categoryListBean")
@SessionScoped
public class CategoryListManagedBean implements Serializable {

	private List<Category> categories;
	private int categoryId;

	@ManagedProperty(value="#{categoryDao}")
	private CategoryDao categoryDao;

	public CategoryDao getCategoryDao() {
		return categoryDao;
	}
	
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
	
	public List<Category> getCategories() {
		return categories;	
	}

	public void setCategories(List<model.Category> categories) {
		this.categories = categories;
	}
	
	public int getCategoryId() {
		return categoryId;	
	}

	public void setCategoryId(int id) {
		this.categoryId = id;
	}
	
	public Category getCategoryById(Long id) {
		for (Category category : getCategories()) {
			if (category.getId().equals(id)) {
				return category;
			}
		}
		return null;
	}

//	public void initCategoryBooks() {
//		HttpServletRequest request=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
//		String value=request.getParameter("id");
//		setBooks(getCategoryDao().getCategoryBooksById(value));
//	}
	
	@PostConstruct
	public void initCategoryList() {
		setCategories(getCategoryDao().getCategories());
	}
}
